package com.rulin.asp.attachment.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.Validate;
import org.javatuples.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rulin.asp.attachment.entity.Attachment;
import com.rulin.asp.attachment.mapper.AttachmentMapper;

@Service
public class AttachServiceImpl implements AttachService {
	
	private static File root = FileUtils.getTempDirectory();
	
	@Autowired
	private AttachmentMapper attachmentMapper;

	/**
	 * 上传图片 并返回图片唯一地址
	 */
	@Override
	@Transactional
	public List<Attachment> uploadPic(List<Pair<String, InputStream>> files,String areaId) throws FileNotFoundException, IOException {
		List<Attachment> list = new ArrayList<Attachment>();
		for (Pair<String, InputStream> pair : files) {
			Attachment attachment = uploadPic(areaId, pair);
			list.add(attachment);
		}
		return list;
	}

	private Attachment uploadPic(String areaId, Pair<String, InputStream> file) throws IOException {
		File directory = new File(root,"PICTURE");
		String address = UUID.randomUUID().toString().toUpperCase();
		
		String fileName = file.getValue0();
		InputStream is = file.getValue1();
//		String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
		String fileType = FilenameUtils.getExtension(fileName).toLowerCase();
		File destFile = new File(directory,address+"."+fileType);
		long fileSize = IO.writeInputStreamToFile(is, destFile);
		
		Attachment attachment = new Attachment();
		attachment.setAttachmentId(address);
		attachment.setAreaId(areaId);
		attachment.setFileName(fileName);
		attachment.setFileType(fileType);
		attachment.setStoreTime(new Date());
		attachment.setFileSize(fileSize);
		attachmentMapper.insert(attachment);
		return attachment;
	}

	/**
	 * 根据地址导出文件到输出流
	 */
	@Override
	public void exportToOutputStream(String address, OutputStream os) {
		Validate.notBlank(address, "address must not blank.");
		Attachment attachment = attachmentMapper.selectByPrimaryKey(address);
		attachment.getFileName();
		String fileType = attachment.getFileType();
		
		try {
			File directory = new File(root,"PICTURE");
			File srcFile = new File(directory, address+"."+fileType); 
			FileInputStream fileInputStream = new FileInputStream(srcFile);
			IOUtils.copy(fileInputStream, os);
			fileInputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Attachment getAttachment(String address) {
		Validate.notBlank(address, "address must not blank.");
		Attachment attachment = attachmentMapper.selectByPrimaryKey(address);
		return attachment;
	}

	@Override
	public List<Attachment> getAttachments(String areaId) {
		Validate.notBlank(areaId, "areaId must not blank.");
		List<Attachment> list = attachmentMapper.selectByAreaId(areaId);
		return list;
	}

	@Override
	public void deleteAttachmentById(String attachmentId) {
		Validate.notBlank(attachmentId);
		Attachment attachment = attachmentMapper.selectByPrimaryKey(attachmentId);
		File directory = new File(root,"PICTURE");
		File destFile = new File(directory,attachmentId+"."+attachment.getFileType());
		if (destFile.isDirectory()) { // 如果是 文件夹
			try {
				FileUtils.deleteDirectory(destFile);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (destFile.isFile()) {// 如果是文件
			destFile.delete();
		}
		attachmentMapper.deleteByPrimaryKey(attachmentId);
	}

	@Override
	public void copyBySourceAreaId(String sourceAreaId, String destAreaId) {
		List<Attachment> list = attachmentMapper.selectByAreaId(sourceAreaId);
		for (Attachment attachment : list) {
			String attachmentId = attachment.getAttachmentId();
			String fileType = attachment.getFileType();
			File directory = new File(root,"PICTURE");
			File srcFile = new File(directory, attachmentId+"."+fileType); 
			try {
				FileInputStream is = new FileInputStream(srcFile);
				String address = UUID.randomUUID().toString().toUpperCase();
				String fileName = attachment.getFileName();
				File destFile = new File(directory,address+"."+fileType);
				long fileSize = 0;
				try {
					fileSize = IO.writeInputStreamToFile(is, destFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				attachment.setAttachmentId(address);
				attachment.setAreaId(destAreaId);
				attachment.setFileName(fileName);
				attachment.setFileType(fileType);
				attachment.setStoreTime(new Date());
				attachment.setFileSize(fileSize);
				attachmentMapper.insert(attachment);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

}
